import re
import sys

import scrapy

sys.path.append(
    r"D:\\Project\\Python\\spider-projects\\scrapy_fuligirl")

from scrapy_fuligirl.items import AlbumsImgItem, AlbumsItem


class AlbumsSpider(scrapy.Spider):
    name = 'albums'
    allowed_domains = ['fuligirl.top']
    start_urls = ['http://fuligirl.top/models/13']

    def parse(self, response):
        """取得相册列表
        """
        grids = response.css("#main > .girl > div")
        for grid in grids:
            album_url = grid.css("img").attrib['src']
            title = grid.css("img").attrib['alt']
            time = grid.css("time").attrib['datetime']
            href = grid.css(".relative > a").attrib['href']
            
            # 专辑id
            id = 0
            match = re.search(r'\d+', href)
            if match:
                id = match.group()
            
            # 专辑
            item = AlbumsItem()
            item['id'] = id
            item['model_id'] = 13
            item['album_url'] = album_url
            item['title'] = title
            item['time'] = time
            item['href'] = href
            
            yield item
            
            yield scrapy.Request(url=href, callback=self.parse_album)
        pass
    
    def parse_album(self, response):
        """获取相册图片
        """
        imgs = response.css(".my-1 > img")
        for img in imgs:
            src = img.attrib['src']
            alt = img.attrib['alt']
            
            
            item = AlbumsImgItem()
            item['url'] = src
            item['title'] = alt
            
            yield item
        
        next = response.css(".pagination > a.next").attrib['href']
        last_page = response.css(".pagination > a.next").attrib['href']
        
        if(next != last_page):
            yield scrapy.Request(url=next, callback=self.parse_album)
            
        pass
